﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AgnisEbmtTranslator;
using System.Threading;
using System.Windows.Forms;

namespace SubmissionTest
{
    class MyTranslatorThreadHandle
    {
        FormTranslationTest  m_form;
        private BindingSource _bindingSource;
        private Boolean storeToDB;
        private int nTransplants;
        private volatile int steps;
        private String currentEbmtDbConnectionString;
        private String currentMappingInfoConnectionString;
        private String currentAgnisStagingConnectionString;
        private int selectedIndex;
        // Main thread sets this event to stop worker thread:
        ManualResetEvent m_EventStop;
        // Worker thread sets this event when it is stopped:
        ManualResetEvent m_EventStopped;

        public MyTranslatorThreadHandle(BindingSource bs, FormTranslationTest mainForm, ManualResetEvent eventStop, ManualResetEvent eventStopped)
        {
            _bindingSource = bs;
            m_form = mainForm;
            m_EventStop = eventStop;
            m_EventStopped = eventStopped;
        }
        
        public BindingSource CurrentBindingSource
        {
            get { return _bindingSource; }
            set { _bindingSource = value; }
        }

        public  int ThreadSteps
        {
            get { return steps; }
            set { steps = value; }
        }

        public FormTranslationTest MainForm
        {
            get { return m_form; }
            set { m_form = value; }
        }

        public Boolean StoreToDataBase
        {
            get { return storeToDB; }
            set { storeToDB = value; }
        }

        public int NumberOfTransplantsToProcess
        {
            get { return nTransplants ; }
            set { nTransplants = value; }
        }

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; }
        }

        public void SetCurrentEbmtDbConnectionString(String cString)
        {
            currentEbmtDbConnectionString = cString;
        }

        public void SetCurrentMappingInfoConnectionString(String cString)
        {
            currentMappingInfoConnectionString = cString;
        }

        public void SetCurrentAgnisStagingConnectionString(String cString)
        {
            currentAgnisStagingConnectionString = cString;
        }


        public void ExecuteTranslationProcess()
        {
            if (_bindingSource.Count > nTransplants)
            {
                TranslatorProcess tProcess = new TranslatorProcess(currentEbmtDbConnectionString, currentMappingInfoConnectionString);
                    for (int i = 0; i < nTransplants; i++)
                    {
                        DataRowView dView;
                        if (nTransplants==1 && selectedIndex >= 0 && selectedIndex < _bindingSource.Count)
                        {
                            dView = (DataRowView)_bindingSource[selectedIndex];
                        }
                        else
                        {
                            dView = (DataRowView)_bindingSource[i];
                        }
                        int id = (int)dView["ID"];
                        int idaa = (int)dView["IDAA"];
                        DateTime dateOfTransplant = (DateTime)dView["IDAABC"];
                        Object[] parameters = new Object[] { id, idaa, dateOfTransplant, (bool)true };
                        m_form.Invoke(m_form.m_DelegateRefreshStatus, new Object[] { parameters });
                        AgnisDAL.ServiceAgnis.FormRevision myGeneratedFormRevision;
                        myGeneratedFormRevision = tProcess.GenerateAgnisFormForPatient(id, idaa, dateOfTransplant, 2450, 2898108, decimal.Parse("1.0", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")), TranslatorProcess.AgnisForm2450DuePeriods.HundredDays);
                        steps = i;
                        if (storeToDB)
                        {
                            AgnisDAL.cAgnisDAL.SetConnectionString(currentAgnisStagingConnectionString);
                            AgnisDAL.cAgnisDAL.SendFormRevisionToDB(myGeneratedFormRevision, 2450, 2898108);

                        }
                        // Make synchronous call to main form.
                        // MainForm.RefreshStatus function runs in main thread.
                        Object[] parametersEnd = new Object[] { id, idaa, dateOfTransplant, (bool)false };
                        m_form.Invoke(m_form.m_DelegateRefreshStatus, new Object[] { parametersEnd });

                        // check if thread is cancelled
                        if (m_EventStop.WaitOne(0, true))
                        {
                            // clean-up operations may be placed here
                            // ...

                            // inform main thread that this thread stopped
                            m_EventStopped.Set();

                            return;
                        }
                }
            }
            // Make synchronous call to main form
            // to inform it that thread finished
            m_form.Invoke(m_form.m_DelegateThreadFinished, null);

        }
    }
}
